<?php

namespace App\Http\Controllers\Member;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
use App\Models\Member\Grade;
use App\Models\Member\Order;
use App\Models\Member\User;

class GradeController extends Controller
{
    // 身份介绍界面
    public function gradePage()
    {
        // 当前身份 / 所有身份和身份样式 / 成为条件(购买要求 / 推荐要求) - 获取当前要求
        $user = Auth::user()->toArray();
        $Grade = new Grade;
        $currentGrade = $user['grade'];// 当前身份
        $grade = $Grade->storeGrade($user['store_id'], 'asc');
        $grade = key_val($grade, 'level');// 以等级为键

        // 身份: 身份/身份名字/身份简介/身份图标/ 样式类型[颜色/背景图] / 初始和截止渐变色 / 背景图
        // 要求: 购买要求 / 推荐要求(只有数目不为0 才显示)
        // 当前满足的情况: 购买要求 / 推荐要求[ 只有未解锁的等级才获取, 已解锁的都按照满了来算 ]
        $gradeArr = [];// 身份
        $recommend = [];// 要求
        foreach ($grade as $key => $val) {
            $gradeArr[$val['level']]['level'] = $val['level'];
            $gradeArr[$val['level']]['title'] = $val['title'];
            $gradeArr[$val['level']]['describe'] = $val['describe'];
            $gradeArr[$val['level']]['icon'] = $val['icon'];
            $gradeArr[$val['level']]['type'] = $val['type'];
            $gradeArr[$val['level']]['bc_img'] = $val['bc_img'];
            $gradeArr[$val['level']]['color_first'] = $val['color_first'];
            $gradeArr[$val['level']]['color_second'] = $val['color_second'];
            
            // 原始要求 - 此处返回 单位为元的数据 前端不需要进行单独处理
            $getAllPrice = $this->getAllPrice($user['id']);
            $recommend[$val['level']]['level'] = $val['level'];
            $recommend[$val['level']]['recommend'] = $this->recommendData($grade, $val, $user, $getAllPrice);// 遍历此处 就可以得出所有前端需要显示的内容
        }
        return $this->successClient(['gradeArr' => $gradeArr, 'recommend' => $recommend], '订单详情获取成功!');  
    }

    // 形成 前端可读 的 要求数据 - 等级数据 和 当前等级
    private function recommendData($grade, $val, $user, $getAllPrice = 0)
    {
        // 文本 / 要求 / 当前要求
        $jf_num = $val['jf_num']/100;
        $jf = $val['jf']/100;
        $team_sales = $val['team_sales']/100;
        $currentGrade = $user['grade'];
        $recommend = json_decode($val['recommend'], 1);
        $gradeStr = $grade[$recommend['grade']]['title'];
        $arr = [];

        //成为合伙人要求 
        if($val["copartner"] == 1){
            //直推人数  
            $arr[0]['text'] = '直推人数达到'.$val['direct_num'].' 名';
            $arr[0]['num'] = $val['direct_num'];
            $arr[0]['haveNum'] = $currentGrade >= $val['level'] ? $val['direct_num'] : $this->getDirectNum($user);// 当前已达到的直推人数
            
            //团队人数
            $arr[1]['text'] = '团队人数达到'.$val['team_num'].' 名 ';
            $arr[1]['num'] = $val['team_num'];
            $arr[1]['haveNum'] = $currentGrade >= $val['level'] ? $val['team_num'] : $this->getTeamNum($user);

            //团队销售额
            $arr[2]['text'] = '团队销售额满'.$team_sales.'元';
            $arr[2]['num'] = $team_sales;
            $arr[2]['haveNum'] = $currentGrade >= $val['level'] ? $team_sales : $this->getTeamSales($user);
            return $arr;
        }

        $arr[0]['text'] = '历史会员订单零售价消费满 '.$jf_num.' 元';
        $arr[0]['num'] = $jf_num;
        $arr[0]['haveNum'] = $currentGrade >= $val['level'] ? $jf_num : $getAllPrice;// 未解锁的等级 获取 历史订单总零售价
        
        if ($recommend['num'] != 0) {
            $arr[1]['text'] = '推荐 '.$recommend['num'].' 名 '.$gradeStr;
            $arr[1]['num'] = $recommend['num'];
            $arr[1]['haveNum'] = $currentGrade >= $val['level'] ? $recommend['num'] : $this->getGradeNum($user, $recommend['grade']);
        } else {
            $arr[1] = ['num' => 0, 'haveNum' => 0, 'text' => '推荐 0 名 会员'];
        }

        //新增积分要求
        $arr[2]['text'] = '用户总积分满'.$jf.'积分';
        $arr[2]['num'] = $jf;
        $arr[2]['haveNum'] = $currentGrade >= $val['level'] ? $jf : $user["jf"]/100;
        return $arr;
    }

    private function getAllPrice($id)
    {
        $Order = new Order;
        $price = $Order->getAllPrice($id);
        return $price;
    }

    public function getGradeNum($user, $grade)
    {
        $User = new User;
        $gradeNum = $User->gradeNum($user, $grade);
        return $gradeNum;
    }

    public function getDirectNum($user){
        $User = new User;
        $directNum = $User->directNum($user);
        return $directNum;
    }

    public function getTeamNum($user){
        $User = new User;
        $teamNum = $User->teamNum($user);
        return $teamNum;
    }

    public function getTeamSales($user){
        $Order = new Order;
        $teamSales = $Order->teamSales($user);
        return $teamSales;
    }
}
